<template>
  <el-dialog :visible.sync="dialogShow" :before-close="check" title="新增">
    <el-form ref="scopeData" label-position="right">
      <el-form-item class="is-required" label="平台名称：" label-width="160px">
        <el-row>
          <el-col :span="6">
            <el-input v-model.trim="platformName" size="mini" placeholder="输入平台名称" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="is-required" label="平台标识：" label-width="160px">
        <el-row>
          <el-col :span="6">
            <el-input v-model="platformMark" size="mini" placeholder="输入平台标识" />
          </el-col>
          <el-col :span="17">
            <div class="notice">按实填写,导入用户信息会根据平台标识进入对应平台,保存后无法更改</div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="积分导入系数：" label-width="160px">
        <el-row>
          <el-col :span="6">
            <el-input v-model="importValue" size="mini" placeholder="输入积分导入系数" />
          </el-col>
          <el-col :span="17" />
          <!-- <div class="notice">按实填写,导入用户信息会根据平台标识进入对应平台,保存后无法更改。</div> -->
          <!-- </el-col> -->
        </el-row>
      </el-form-item>
      <el-form-item label="运营商用户导入路径：" label-width="160px">
        <el-row>
          <el-col :span="6">
            <el-input v-model="path" size="mini" placeholder="运营商用户导入路径：" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item class="btn">
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button
              type="primary"
              round
              size="mini"
              style="width:50%"
              @click="savePlat"
            >保存</el-button>
          </el-col>
          <el-col :span="6">
            <el-button
              type="primary"
              plain
              size="mini"
              round
              style="width:50%"
              @click="check"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import secret from '@/utils/secret'
import { addPlat } from '@/api/platformManage'
import { plusNum, numAndWord } from '@/utils/validate'
import { mapGetters } from 'vuex'
export default {
  name: 'EditAdd',
  props: {
    // 遮罩
    addScope: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogShow: false, // 遮罩
      path: '',
      platformId: null, // 平台ID
      platformName: null, // 平台名称
      platformMark: null, // 平台标识
      importValue: null // 积分导入系数
    }
  },
  computed: {
    ...mapGetters(['token'])
  },
  watch: {
    // 遮罩监听
    addScope(newVal) {
      this.dialogShow = newVal
    }
  },
  methods: {
    // 给父组件传值
    check() {
      this.$emit('closeDialog')
      this.dialogShow = false
      this.platformName = null
      this.platformMark = null
      this.importValue = null
      this.path = ''
    },
    // 保存
    savePlat() {
      if (!this.platformName) {
        this.$message({
          message: '平台名称不可为空',
          type: 'error'
        })
      } else if (!this.platformMark) {
        this.$message({
          message: '平台标识不可为空',
          type: 'error'
        })
      } else {
        if (!this.importValue) {
          this.importValue = 1
        }
        const all = {
          // token: this.token,
          platformName: this.platformName,
          platformMark: this.platformMark,
          importValue: this.importValue,
          path: this.path
        }
        // 校验是否为数字或字母
        const real = numAndWord(this.platformMark)
        if (real) {
          const realNum = plusNum(this.importValue)
          if (realNum) {
            console.log('请求参数')
            console.log(all)
            addPlat(secret.Encrypt(all)).then(response => {
              console.log('返回数据')
              console.log(response)
              if (response.resultCode !== '0000') {
                this.$message({
                  message: response.resultDesc,
                  type: 'error'
                })
              } else {
                this.$message({
                  message: response.resultDesc,
                  type: 'success'
                })
                this.check()
              }
            })
          } else {
            this.$message({
              message: '积分导入系数只可输入正数,小数点后保留4位数字',
              type: 'error'
            })
          }
        } else {
          this.$message({
            message: '平台标识仅可填写英文或数字',
            type: 'error'
          })
        }
      }
    }
  }
}
</script>
<style scoped>
  .notice{
    font-size: 12px;
    color: red;
    padding-left: 10px;
  }
  .btn{
    margin-bottom: -20px !important;
    padding-bottom: 10px;
  }
</style>
